package com.youdoneed.community.forum.service;

import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.group.service.ForumgroupService;
import com.youdoneed.community.systemReport.SystemReportVo;
import com.youdoneed.user.feign.UserFeignClient;
import com.youdoneed.user.vo.UserStatistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by lid on 2017/8/5 0005.
 * 获取系统数据服务接口
 */
@Service
public class SystemReportService {

    private final Logger logger = LoggerFactory.getLogger(SystemReportService.class);

    @Autowired
    ForumreportService forumreportService;

    @Autowired
    ForummessageService forummessageService;

    @Autowired
    ForumgroupService forumgroupService;

    @Autowired
    UserFeignClient userFeignClient;

    /**
     * 获取系统的今日情况
     *
     * @return
     * @author lid
     * @date 2017.8.5
     */
    @Transactional
    public SystemReportVo getSystemReport() {
        try {
            Integer totalReportCount = forumreportService.getReportMsgsCount();
            logger.info("totalReportCount:" + totalReportCount);

            Integer newMsgsCount = forummessageService.countTodayNewMsgs();
            logger.info("newMsgsCount:" + newMsgsCount);

            Integer totalMsgsCount = forummessageService.countAllMsgs();
            logger.info("totalMsgsCount:" + totalMsgsCount);

            Integer newGroupsCount = forumgroupService.countTodayNewGroups();
            logger.info("newGroupsCount:" + newGroupsCount);

            Integer totalGroupsCount = forumgroupService.countAllGroups();
            logger.info("totalGroupsCount:" + totalGroupsCount);

            UserStatistics userStatistics = userFeignClient.userStatistics().getData();

            SystemReportVo vo = new SystemReportVo();
            vo.setTotalReportCount(totalReportCount);
            vo.setNewMsgsCount(newMsgsCount);
            vo.setTotalMsgsCount(totalMsgsCount);
            vo.setNewGroupCount(newGroupsCount);
            vo.setTotalGroupCount(totalGroupsCount);
            if (null != userStatistics) {
                vo.setNewUsersCount(userStatistics.getNewUserToday());
                vo.setTotalUsersCount(userStatistics.getTotalUser());
            }

            return vo;
        } catch (Exception e) {
            logger.info("获取系统今日情况失败！" + e);
            return null;
        }

    }
}
